<template>
    <div class="vipUpdate">
        <el-form v-if="type==='update'">
            <el-form-item prop="vipType">
                <div class="item">
                    <div class="label">
                        <div style="line-height:20px">会员类型：</div>
                        <!--<div style="line-height:20px"><a>校园特惠&nbsp;&nbsp;&nbsp;</a></div>-->
                    </div>
                    <div class="vipList name">
                        <div>
                            <el-checkbox-group class="check-group" v-model="vipType"
                                               @change="vipTypeChange">
                                <el-checkbox class="check-box" v-for="vip in vipList" :label="vip.name"
                                             :value="vip.name" :disabled="currentLevel>=vip.level"
                                             :key="vip.name">
                                    <div class="info vip-icon" :class="vip.icon">
                                        <div>
                                            <div class="price">￥&nbsp;{{vip.price}}</div>
                                        </div>
                                    </div>
                                </el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </div>
                </div>
            </el-form-item>
            <el-form-item prop="number">
                <div class="item">
                    <div class="label">最高参会人数：</div>
                    <div class="name">
                        <div class="maxNum">{{vipInfo.maxPerson!==-1?vipInfo.maxPerson?vipInfo.maxPerson:0 + '人':'无限制'}}
                        </div>
                    </div>
                </div>
            </el-form-item>
            <el-form-item>
                <div class="item">
                    <div class="label">功能使用数量：</div>
                    <div class="name">
                        {{vipInfo.moduels!==-1?vipInfo.moduels?vipInfo.moduels:0 + ' 个':'无限制'}}
                    </div>
                </div>
            </el-form-item>
            <el-form-item>
                <div class="item">
                    <div class="label">存储空间：</div>
                    <div class="name">
                        {{vipInfo.storage}}&nbsp;G
                    </div>
                </div>
            </el-form-item>
            <el-form-item>
                <div class="item">
                    <div class="label">开通期限：</div>
                    <div class="name">
                        {{restDay}}&nbsp;天
                    </div>
                </div>
            </el-form-item>
            <el-form-item>
                <div class="item">
                    <div class="label">实付金额：</div>
                    <div class="name amount">
                        <div>
                            <!--￥{{vipInfo.price}}-->
                            ￥{{finalPrice}}
                        </div>
                    </div>
                </div>
            </el-form-item>
            <div class="tip">
                <el-checkbox style="margin-right: 5px" v-model="isAgree"></el-checkbox>
                <span style="color: red">同意</span><a style="color: #1989FA" href="#">《智汇互动系统使用条款》</a>中的各项内容
                <span style="color: #aaa">（注：支付成功后，不支持退款）</span>
            </div>
            <div class="buttons">
                <!--<el-button type="info" @click="closeModal">取消</el-button>-->
                <el-button size="normal" type="primary" @click="vipUpdate">立即开通</el-button>
            </div>
        </el-form>
        <el-form v-else>
            <el-form-item prop="vipType">
                <div class="item">
                    <div class="label">
                        <div style="line-height:20px">会员类型：</div>
                        <!--<div style="line-height:20px"><a>校园特惠&nbsp;&nbsp;&nbsp;</a></div>-->
                    </div>
                    <div class="vipList name">
                        <div>
                            <el-checkbox-group class="check-group" v-model="vipType"
                                               @change="vipTypeChange">
                                <el-checkbox class="check-box" v-for="vip in vipList" :label="vip.name"
                                             :value="vip.name" v-if="currentLevel===vip.level||currentLevel===1"
                                             :key="vip.name">
                                    <div class="info">
                                        <div class="info vip-icon" :class="vip.icon">
                                            <div>
                                                <div class="price">￥&nbsp;{{vip.price}}</div>
                                            </div>
                                        </div>
                                    </div>
                                </el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </div>
                </div>
            </el-form-item>
            <el-form-item prop="number">
                <div class="item">
                    <div class="label">最高参会人数：</div>
                    <div class="name">
                        <div class="maxNum">{{vipInfo.maxPerson!==-1?vipInfo.maxPerson?vipInfo.maxPerson:0 + '人':'无限制'}}
                        </div>
                    </div>
                </div>
            </el-form-item>
            <!--<el-form-item prop="selectedTime">-->
            <!--<div class="item">-->
            <!--<div class="label">同时进行的活动：</div>-->
            <!--<div class="name">-->
            <!--{{vipInfo.sceneTime}}&nbsp;场-->
            <!--</div>-->
            <!--</div>-->
            <!--</el-form-item>-->
            <el-form-item>
                <div class="item">
                    <div class="label">功能使用数量：</div>
                    <div class="name">
                        {{vipInfo.moduels!==-1?vipInfo.moduels?vipInfo.moduels:0 + ' 个':'无限制'}}
                    </div>
                </div>
            </el-form-item>
            <!--<el-form-item>-->
            <!--<div class="item">-->
            <!--<div class="label">可发布次数：</div>-->
            <!--<div class="name">-->
            <!--{{restTimes}}&nbsp;次-->
            <!--</div>-->
            <!--</div>-->
            <!--</el-form-item>-->
            <el-form-item>
                <div class="item">
                    <div class="label">存储空间：</div>
                    <div class="name">
                        {{vipInfo.storage>-1?vipInfo.storage:'0'}}&nbsp;G
                    </div>
                </div>
            </el-form-item>
            <el-form-item>
                <div class="item">
                    <div class="label">开通期限：</div>
                    <div class="name">
                        {{restDay>-1?restDay:'0'}}&nbsp;天
                    </div>
                </div>
            </el-form-item>
            <el-form-item>
                <div class="item">
                    <div class="label">实付金额：</div>
                    <div class="name amount">
                        <div>
                            <!--￥{{vipInfo.price}}-->
                            ￥{{finalPrice>-1?finalPrice:'0'}}
                        </div>
                    </div>
                </div>
            </el-form-item>
            <div class="tip">
                <el-checkbox style="margin-right: 5px" v-model="isAgree"></el-checkbox>
                <span style="color: red">同意</span><a style="color: #1989FA" href="#">《智汇互动系统使用条款》</a>中的各项内容
                <span style="color: #aaa">（注：支付成功后，不支持退款）</span>
            </div>
            <div class="buttons">
                <!--<el-button type="" @click="closeModal">取消</el-button>-->
                <el-button type="danger" @click="vipUpdate">立即开通</el-button>
            </div>
        </el-form>
    </div>
</template>

<script>
    import QRCode from 'qrcodejs2';

    export default {
        name: 'vipUpdate',
        props: {
            closeModal: {
                required: true,
            },
            type: {
                required: true,
            },
            memberInfo: {},
        },
        data() {
            return {
                vipInfo: {},
                vipList: [],
                vipType: [],
                isAgree: true,
                currentLevel: '',
                currentInfo: {},
            };
        },
        computed: {
            restDay() {
                return this.type === 'update' && this.currentInfo.endDate && this.currentInfo.startDate ? Math.ceil((this.currentInfo.endDate - new Date()) / (24 * 3600 * 1000)) : this.vipInfo.days;
            },
            restTimes() {
                let usedTimes;
                this.vipList.forEach(item => {
                    if (item.level === this.currentInfo) {
                        usedTimes = item.sceneTime - this.currentInfo.spurplusTimes;
                    }
                });
                return this.vipInfo.sceneTime - usedTimes;
            },
            finalPrice() {
                return this.vipInfo.price && this.type === 'update' ? (this.restDay * (this.vipInfo.price - this.getCurrentPrice()) / this.vipInfo.days).toFixed(2) : this.vipInfo.price;
            },
        },
        mounted() {
            let info = this.memberInfo;
            this.currentInfo = info;
            this.currentLevel = info.level;
            this.getVipList();
            this.$bus.$on('payStatus', (status) => {
                console.log('vipupdate', status);
            });

        },
        methods: {
            vipTypeChange(info) {
                this.vipInfo = this.getSelectedInfo(info[info.length - 1]);
                if (this.vipType.length > 1) {
                    this.vipType = [this.vipType[1]];
                }
            },
            vipUpdate() {
                if (!this.isAgree) {
                    this.$message.info('请阅读《智汇互动系统使用条款》，并选择同意此条款');
                    return false;
                }
                if (this.vipType.length < 1) {
                    this.$message.info('请选择会员类型');
                    return false;
                }
                if (this.vipType[0] === '普通用户') {
                    this.$message.info('系统默认为普通用户，不需要再次开通');
                    return false;
                }
                let info = this.vipInfo;
                let param = {
                    title: (this.type !== 'update' ? (this.currentLevel < 2 ? '开通' : '续费') : '升级') + info.name,
                    type: this.type === 'update' ? 3 : this.currentLevel < 2 ? 2 : 5,
                    totalPrice: this.finalPrice,
                    finalPrice: this.finalPrice,
                    detail: {
                        current: JSON.parse(sessionStorage.getItem('member')),
                        update: info,
                    },
                };
                this.$api.post('/order/add', param, success => {
                    this.closeModal();
                    this.$bus.$emit('orderPay', success);
                }, fal => {
                    this.$message.error(fal);
                });

            },
            getSelectedInfo(text) {
                let info = '';
                this.vipList.forEach(item => {
                    item.name === text ? info = item : '';
                });
                return info;
            },
            getVipList() {
                let vipList = sessionStorage.getItem('vipList');
                if (vipList && JSON.parse(vipList)) {
                    this.vipList = JSON.parse(vipList);
                    this.setCurVip();
                } else {
                    this.$api.get('/member/list', null, success => {
                        this.vipList = success;
                        sessionStorage.setItem('vipList', JSON.stringify(success));
                        this.setCurVip();
                    }, fal => {
                        this.$message.error(fal);
                    });
                }

            },
            getCurrentPrice() {
                let price = '';
                this.vipList.forEach(item => {
                    if (this.currentLevel === item.level) {
                        price = item.price;
                    }
                });
                return price;
            },
            setCurVip() {
                this.vipList.forEach(item => {
                    if (this.type === 'update') {
                        if (this.currentLevel > 3) {
                            if (this.currentLevel - 1 === item.level - 2) {
                                this.vipInfo = item.level;
                                this.vipType = [item.name];
                            }
                        } else if (this.currentLevel - 1 === 2 && item.level - 2 === 2) {
                            this.vipInfo = item;
                            this.vipType = [item.name];
                        }
                    } else {
                        if (this.currentLevel > 3) {
                            if (this.currentLevel - 1 === item.level - 2) {
                                this.vipInfo = item.level;
                                this.vipType = [item.name];
                            }
                        } else if ( item.level - 1 === 2) {
                            this.vipInfo = item;
                            this.vipType = [item.name];
                        }
                    }
                });
            },
        },
    };
</script>

<style lang="scss" scoped>
    .vipUpdate {
        /deep/ .el-form-item {
           margin-bottom: 0!important;
        }
        .item {
            margin: 10px 0;
            display: flex;
            .label {
                margin-right: 10px;
                width: 140px;
                text-align: right;
            }
            .name {
                width: calc(100% - 150px);
            }

        }
        .amount {
            font-size: 25px;
            color: #FF0000;
        }
        .vipList {
            & > div {
                display: flex;
                justify-content: flex-start;
                flex-wrap: wrap;
            }
            /deep/ .check-box {
                width: 120px;
                height: 70px;
                border: none;
                transition: all .3s;
                margin-bottom: 20px;
                top: 0;
            }
            /deep/ .el-checkbox.check-box.is-checked {
                top: -10px;
                box-shadow: 3px 4px 4px 0 rgba(0, 0, 0, 0.20);
            }
            /deep/ .is-disabled {
                .price {
                    color: #aaaaaa;
                }
            }
            /deep/ .is-disabled:after {
                content: ' ';
                position: absolute;
                top: 0;
                left: 0;
                height: 100%;
                width: 100%;
                cursor: not-allowed;
                background: rgba(170, 170, 170, 0.4);
            }
            /deep/ .is-checked {
                span.el-checkbox__inner {
                    top: 5px;
                    width: 35px;
                    height: 20px;
                    @include left-radius(10px);
                    text-align: center;
                    background: rgba(55, 104, 156, 0.30);
                    border: none;
                }
                span.el-checkbox__inner:after {
                    width: 8px;
                    height: 16px;
                    border-width: 2px;
                    left: 14px;
                    top: -2px;
                }
            }
        }
        .info {
            width: 100%;
            height: 100%;
            position: relative;
            overflow: hidden;
            .price {
                font-size: 12px;
                padding: 10px 0 10px 30px;
                position: absolute;
                bottom: 0;
                left: 0;
                width: 100%;
            }
        }

        .tip {
            text-align: center;
            padding: 10px;
            transform: scale(0.9);
        }

        .buttons {
            text-align: right;
            padding: 20px;
            margin: 20px 0;
            button {
                width: 150px;
                background: #FA1919;
                border-radius: 20px;
                border: none;
                transition: all .3s;
                top: 0;
            }
            button:hover {
                top: -2px;
                box-shadow: 3px 4px 4px 0 rgba(0, 0, 0, 0.20);
                background: #ff3535;
                color: #fefefe;
            }
        }
    }

    .bank {
        @include container-border;
        border-radius: 10px;

        .bank-info {
            padding: 10px 20px;
            font-size: 16px;
        }

    }

    .can-select {
        user-select: text;
    }

    //会员等级icon和功能列表icon
    .vip-icon {
        background-repeat: no-repeat;
        background-size: cover;
    }

    .vip-2 {
        background-image: url("../../../assets/imgs/vipImg/level2.png");
        color: rgba(72,83,108,0.70);
    }

    .vip-3 {
        background-image: url("../../../assets/imgs/vipImg/level3.png");
        color: rgba(143,97,11,0.70);
    }

    .vip-4 {
        background-image: url("../../../assets/imgs/vipImg/level4.png");
        color: rgba(90,95,121,0.70);
    }

    .vip-5 {
        background-image: url("../../../assets/imgs/vipImg/level5.png");
        color: rgba(64,119,176,0.70);
    }

</style>